package com.UI;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddFacultyHours extends Activity{
	private String facultyType;
	private String courseNo;
	private String facultyID;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle extras = getIntent().getExtras();
		courseNo = extras.getString("CourseNum");

		setContentView(R.layout.add_faculty_hours);
		
		// create a model instance
		final StudentDBModel newFacultyHours = new StudentDBModel(this);
		
		//create the spinner to hold the list of faculty types that can be added
		Spinner facultyTypeSpinner = (Spinner) findViewById(R.id.addFacultyTypeSpinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.AddFacultyTypeArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		facultyTypeSpinner.setAdapter(adapter);

		// Set up a call back for the spinner
		facultyTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do nothing.
			}

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				final Spinner selectedEvent = (Spinner) findViewById(R.id.addFacultyTypeSpinner);
				facultyType = selectedEvent.getSelectedItem().toString();
			}

		});
		
		Button btnSubmit = (Button) findViewById(R.id.btnFacHoursInfoSubmit);
		btnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// Get the values from the user and send them to the DB
				ContentValues values = new ContentValues();
		
				EditText txtFacName = (EditText) findViewById(R.id.txtFacName);
				
				String tempFacName = txtFacName.getText().toString().replaceAll(" ", "");
				
				if(tempFacName.length() == 0) { 
		 		   AlertDialog alertDialog = new AlertDialog.Builder(AddFacultyHours.this).create();	        			
		 		   alertDialog.setTitle("Warning");	        		  
		 		   alertDialog.setMessage("Please Enter a Faculty Member's Name");	        		  	        		  
		 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		 			   public void onClick(DialogInterface dialog, int which) {
		 				   return;
		 			   }
		 		   }); 
				   alertDialog.show();
				}
				else if(facultyType.length() == 0){
			 		   AlertDialog alertDialog = new AlertDialog.Builder(AddFacultyHours.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please select a Faculty Type");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 			   public void onClick(DialogInterface dialog, int which) {
			 				   return;
			 			   } 
			 		   }); 
					  alertDialog.show();
				}
				else{
					facultyID = courseNo + " - " + txtFacName.getText().toString();
					values.put("FacCourseComboID", facultyID);
					txtFacName = (EditText) findViewById(R.id.txtFacName);
					values.put("FacName",txtFacName.getText().toString());
					values.put("CourseId", courseNo);
					values.put("FacType", facultyType);
			
					EditText txtFacLocation = (EditText) findViewById(R.id.txtFacLocation);
					values.put("FacLocation", txtFacLocation.getText().toString());
			
					EditText txtFacHours = (EditText) findViewById(R.id.txtFacHours);
					values.put("FacHours", txtFacHours.getText().toString());
			
					// use the model to store the data
					newFacultyHours.addNewFacultyHours(values);
			
					// tell the user the assignment was added to the database
					TextView lblResults = (TextView) findViewById(R.id.lblAddFacHoursResult);
					lblResults.setText(getString(R.string.FacHoursAdded));
					
					//finish the activity to return to the calling event
					AddFacultyHours.this.finish();
				}
			}
		});
		
		// register this activity as a button event listener
		Button btnGoBack = (Button) findViewById(R.id.btnBack);
		btnGoBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				AddFacultyHours.this.finish();
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(AddFacultyHours.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				AddFacultyHours.this.finish();
			}
		});
	}
}
